package tutorial.api.new_api.front_page;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import public_util.model.Const4PublicDataBase;
import public_util.model.ReturnData;
import public_util.servlet.ServletParent;
import public_util.tool_4_handle.Tool_4_Servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Controller("/application_4_tutorial_front_page")
public class Tutorial4FrontPageServlet extends ServletParent {

    private static final long serialVersionUID = 1L;
    private static Logger logger = Logger.getLogger(Tutorial4FrontPageServlet.class);




    /**
     * @see HttpServlet#HttpServlet()
     */
    public Tutorial4FrontPageServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }



    /**
     */
    @RequestMapping("/application_4_tutorial_front_page")
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String methodName = request.getParameter(Const4PublicDataBase.PARA_invoke_method);
        if (methodName!= null)
        {
            logger.info("--2--");
            super.doPost(request, response);
            return;
        }
        logger.info("--1--");

        //--提前打印出来
        ReturnData data  = new ReturnData();

        data.setStatus(0);
        printRes(response, data);
    }



    /**
     * 测试URL: application_4_tutorial_front_page?invoke_method=show_test_demo&time=1000
     */
    public void show_test_demo(HttpServletRequest request, HttpServletResponse response) {

        String time = request.getParameter("time");
        ReturnData data =new ReturnData();

        SqlSessionFactory factory = getSqlSession();


        if (time == null)
        {
            data.setMsg("指令已经执行完毕");
            printRes(response, data);
            return;
        }
        Tutorial4FrontPageTool.showDemo(factory, this, response, request);
        int time_int = Integer.valueOf(time);

        try {
            Thread.sleep(time_int);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Object shop_info = Tool_4_Servlet.getAttribute(null, request.getSession(), Const4PublicDataBase.MEM_4_self_public_agent_info);
        data.setBig_data(shop_info);
        data.setStatus(0);
        printRes(response, data);
    }

}